### =========================================================================
### I. Preparation and Settings ####
### =========================================================================

### Please load this function which corrected bugs in original CoordinateCleaner package.

source("./functions/clean_coordinates_debug.R")

### Set the working path and output folders for name correcting and coordinate cleaning 

wkpath <- ""
setwd(wkpath)

name.correct.folder <- "./2.1_name_correction/"
cc.cleaned <- "./2.2_cleaning_cc/cleaning_cc_occurrences/"
cc.cleaned.report <- "./2.2_cleaning_cc/cleaning_cc_report/"
cc.cleaned.detail <- "./2.2_cleaning_cc/cleaning_cc_detail/"

### Load your occurrence data. 
### PLEASE ensure that there should include three columns: "sciName", "x", "y", 
### representing original species names, longitude, and latitude
occ.all <- readRDS("./example/allGBIFandBIEN_Occurrences.rds")
### Or you can use different functions to load data in different formats, e.g.:
# occ.all <- read.csv(".csv")
# occ.all <- read.table(".txt", header = T)


### =========================================================================
###
### II. Please run following codes to do species name correction ####
###     with The Plant List (TPL, http://www.theplantlist.org/)
###
### PLEASE BE AWARE: 
###
###     TPL had stopped updating. If you need more updated species name reference databases,
###   please try R package "WorldFlora" from The World Flora Online (http://www.worldfloraonline.org/)
###   or request species name list from The Catalogue of Life (https://catalogueoflife.org/).
###
### =========================================================================

#### 1. Preparation ####

### install packages globally
# set a local mirror server
options(repos=structure(c(CRAN="https://stat.ethz.ch/CRAN/")))

# Packages to load
packages <- c("Taxonstand", "CoordinateCleaner", "rgdal", "sp")

# Install missing ones and load all packages
for (p in packages) {
  if(!library(package = p, logical.return = TRUE, character.only = TRUE)){
    install.packages(p)
    library(package = p, character.only = TRUE)
  } else {   
    library(package = p, character.only = TRUE) 
  }
}

if(!dir.exists(name.correct.folder)) {dir.create(name.correct.folder, recursive = T)}
if(!dir.exists(cc.cleaned)) {dir.create(cc.cleaned, recursive = T)}
if(!dir.exists(cc.cleaned.report)) {dir.create(cc.cleaned.report, recursive = T)}
if(!dir.exists(cc.cleaned.detail)) {dir.create(cc.cleaned.detail, recursive = T)}

#### 2. Species name correction ####

spname0 <- as.character(unique(na.omit(occ.all[ ,"Taxon"]$Taxon)))
name.correct0 <- TPL(spname0)

name.correct <- merge(occ.all, name.correct0)
# saveRDS(name.correct, file.path(name.correct.folder, "Occurrence_NameCorrected_allInfo.rds"))

# Extract accepted species names and remove hybrid species
name.corrected <- name.correct[which(name.correct$New.Taxonomic.status == "Accepted" | name.correct$New.Taxonomic.status == "Synonym"), ]
name.corrected <- name.corrected[which(name.corrected$New.Hybrid.marker == ""), ]

name.corrected$name <-  paste(name.corrected$New.Genus, name.corrected$New.Species)
occ.corrected <- name.corrected[ ,c("Family", "New.Genus","name", "x", "y")]
colnames(occ.corrected) <- c("family", "genus", "name", "x", "y")
occ.corrected <- na.omit(unique(occ.corrected))

saveRDS(occ.corrected, file.path(name.correct.folder, "Occurrence_NameCorrected.rds"))


### =========================================================================
###
### III. Please run following codes to do species occurrence cleaning with CoordinateCleaner ####
###
### =========================================================================

occ.corrected <- readRDS(file.path(name.correct.folder, "Occurrence_NameCorrected.rds"))

occ.corrected$x <- round(occ.corrected$x,3)
occ.corrected$y <- round(occ.corrected$y,3)
occ.corrected <- unique(occ.corrected)

spcleaning <- clean_coordinates_debug(occ.corrected, lon="x", lat="y", species="name", 
                          tests = c("capitals", "gbif", "institutions", "outliers", "zeros"), 
                          inst_rad = 1000, outliers_size = 10, range_rad = 200000, 
                          verbose = TRUE, report=TRUE, value="spatialvalid", 
                          reportadd=file.path(cc.cleaned.report,"CoordinateCleaner_Report.txt"))

saveRDS(spcleaning, paste0("./CoordinateCleaner_cleaningTags.rds"))
  
spcleaned <- spcleaning[spcleaning$.summary == TRUE, c("family", "name", "x", "y")]
spcleaned <- as.data.frame(unique(spcleaned))

### You can choose a way to save the cleaned records, either by:
# save all records in one file:
saveRDS(spcleaned, file.path(cc.cleaned, "Occurrence_cleaned_cc.rds"))

# or save in seperate files by species:
spname_cc_uni <- unique(spcleaned$name)
spfam_cc_uni <- unique(spcleaned$family)

for (fam in spfam_cc_uni){
if (!dir.exists(file.path(cc.cleaned, fam))){
  dir.create(file.path(cc.cleaned, fam), recursive = T)
}}

for (i in 1:length(spname_cc_uni)){
  spcleaned_i <- spcleaned[spcleaned$name == spname_cc_uni[i], ]
  write.csv(spcleaned_i, file.path(cc.cleaned, spcleaned_i$family[i], paste0(spcleaned_i$family[i], "_", spname_cc_uni[i], ".csv")), row.names = F)
}
